<?php

class SJB_Admin_Classifieds_ExportListings extends SJB_Function
{
	public function isAccessible()
	{
		$this->setPermissionLabel('export_listings');
		return parent::isAccessible();
	}

	public function execute()
	{
		ini_set('max_execution_time', 0);
		$tp = SJB_System::getTemplateProcessor();
		$errors = array();

		$listing_type_id = isset($_REQUEST['listing_type']) ? $_REQUEST['listing_type']['equal'] : 0;
		$listing_type_id = SJB_Request::getVar('listing_type_id', $listing_type_id);
		$export_properties = SJB_Request::getVar('export_properties', array());

		$listing = SJB_ExportController::createListing($listing_type_id);
		$criteria = SJB_SearchFormBuilder::extractCriteriaFromRequestData($_REQUEST, $listing);

		$search_form_builder = new SJB_SearchFormBuilder($listing);
		$search_form_builder->registerTags($tp);
		$search_form_builder->setCriteria($criteria);

		if ($_SERVER['REQUEST_METHOD'] == 'POST') {
			if (empty($export_properties)) {
				$errors['EMPTY_EXPORT_PROPERTIES'] = true;
			}
			else {
				$searcher = new SJB_ListingSearcher();
				$search_aliases = SJB_ExportController::getSearchPropertyAliases();
				$found_listings_sid = $searcher->getObjectsSIDsByCriteria($criteria, $search_aliases);

				if (empty($found_listings_sid)) {
					$errors['EMPTY_EXPORT_DATA'] = true;
				}
				else
				{
					$result = SJB_ExportController::createExportDirectories();
					if ($result === true) {
						$export_properties['extUserID'] = 1;
						$export_aliases = SJB_ExportController::getExportPropertyAliases();
						$export_data = SJB_ExportController::getExportData($found_listings_sid, $export_properties, $export_aliases);

						SJB_ExportController::changeTreeProperties($export_properties, $export_data);
						SJB_ExportController::changeListProperties($export_properties, $export_data);
						SJB_ExportController::changePicturesProperties($export_properties, $export_data);
						SJB_ExportController::changeFileProperties($export_properties, $export_data, 'file');
						SJB_ExportController::changeFileProperties($export_properties, $export_data, 'video');
						SJB_ExportController::changeComplexFileProperties($export_properties, $export_data, 'complexfile');
						SJB_ExportController::changeComplexFileProperties($export_properties, $export_data, 'complexvideo');
						SJB_ExportController::changeLocationProperties($export_properties, $export_data);

						$file_name = 'export.xls';
						SJB_ExportController::makeExportFile($export_properties, $export_data, $file_name);
						if (!file_exists(SJB_System::getSystemSettings('EXPORT_FILES_DIRECTORY') . "/{$file_name}"))
							$errors['CANT_CREATE_EXPORT_FILES'] = true;
						else
							SJB_HelperFunctions::redirect(SJB_System::getSystemSettings('SITE_URL') . '/archive-and-send-export-data');
					}
				}
			}
		}

		$listing_properties_id = SJB_ListingManager::getAllListingPropertiesID($listing_type_id);

		$tp->assign('errors', $errors);
		$tp->assign('properties_id', $listing_properties_id);
		$tp->assign('selected_listing_type_id', $listing_type_id);
		$tp->display('export_listings.tpl');
	}
}